%% Section 0: Description of the file
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Explanation of what the file does
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Compute outcomes of the model for different values delta
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Input files needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% InputData.xlsx
% ProcessedData.xlsx
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Matlab functions invoked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% NEST_OuterAlgoCF_FUNC

%% Section 1: Computations

clear all
clc

mi       = 1;
optdelta = 4;
ka       = 6.547537;
nu       = 0.537175;
tectime  = [0.646317,0.621760,1.632136,1.398806,0.942922,0.790867,...
            0.862154]';
tecsect  = [0.139465,0.077186,0.077833,0.053073,0.100555,0.070690,...
            0.070298,0.081616,0.091727,0.083907,0.074990,0.078659]';
ba       = 0.95;
zrs      = 0.5;
AECR     = [0,0,0,0,0];      
AECRS    = [0,0,0,0,0];
aeatw    = 1;
delvec   = [0.920,0.940,0.950,0.960,0.965,0.970,0.9725,0.975,0.9775,...
            0.980,0.981,0.982,0.983,0.984,0.985,0.9855,0.986,0.9865,0.987,...
            0.9875,0.988,0.9885,0.989,0.9895,0.99,0.9905,0.990801,0.991,0.9915,...
            linspace(0.992,0.995,15)];
K        = length(delvec);
betahuge = NaN(7,30,K);
numrschu = NaN(4,100,K);
MWCWhuge = NaN(K,1);
MWCUhuge = NaN(K,1);
CUSEmphu = NaN(K,100);
CUSLSuhu = NaN(K,100);
CUSUnehu = NaN(K,100);
USInfhu  = NaN(K,100);

parpool(K,'IdleTimeout',6000,'SpmdEnabled',false)
disp('Pool opened succesfully')

parfor counter=1:K
    delta                 = delvec(counter); %#ok<*PFBNS>               
   [betabig,MWCW,MWCU,CUSEmp,CUSLSu,CUSUne,numrscon,USInfla] = ...
          NEST_OuterAlgoCF_FUNC(delta,ka,nu,mi,tectime,...
          tecsect,optdelta,ba,zrs,AECR,AECRS,aeatw)
    betahuge(:,:,counter) = betabig;
    numrschu(:,:,counter) = numrscon;
    MWCWhuge(counter,1)   = MWCW;
    MWCUhuge(counter,1)   = MWCU;
    CUSEmphu(counter,:)   = CUSEmp;
    CUSLSuhu(counter,:)   = CUSLSu;
    CUSUnehu(counter,:)   = CUSUne;
    USInfhu(counter,:)    = USInfla;

    disp(['Delta=',num2str(delta,'%.3f')])
end

save('ResultsDeltaTable')
delete(gcp('nocreate'))
